﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ctdFunction
{
    public class Function
    {
        public static byte[] strToToHexByte(string hexString)       //////3132 ->\x31\x32
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }

        public static long strToToLong(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            long retLong = (returnBytes[0] << 8) + returnBytes[1];
            return retLong;
        }

        public static string _memcpy(string src, string off, string len)
        {
            byte[] b1 = strToToHexByte(off);
            byte[] b2 = strToToHexByte(len);

            int len1 = b1[0] * 2;
            int len2 = b2[0] * 2;

            return src.Substring(len1, len2);
        }

        /// <summary>
        /// 31-0x31
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static byte[] HexStrToByte(string hex)//字串符转换成16进制byte[]
        {
            hex = hex.Replace(" ", "");
            return Enumerable.Range(0, hex.Length)
                             .Where(x => x % 2 == 0)
                             .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                             .ToArray();
        }

        /// <summary>
        /// 0x31->31 
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string ByteToHexStr(byte[] bytes, int byteLen = 0) // 0xae00cf => "AE00CF "
        {

            string hexString = string.Empty;

            if (bytes != null)
            {
                StringBuilder strB = new StringBuilder();
                if (byteLen > 0 && byteLen < bytes.Length)
                {
                    for (int i = 0; i < byteLen; i++)
                    {
                        strB.Append(bytes[i].ToString("X2"));
                    }
                }
                else
                {
                    for (int i = 0; i < bytes.Length; i++)
                    {
                        strB.Append(bytes[i].ToString("X2"));
                    }
                }

                hexString = strB.ToString();

            }
            return hexString;
        }

        public static string Analyticdata(string _data)
        {
            try
            {
                string retData = "";
                string[] str = _data.Split(',');
                for (int i = 0; i < str.Length; i++)
                {
                    string tmp = str[i];
                    retData += "\n" + tmp;
                    string[] data2 = tmp.Split(':');
                    string tag = data2[0];
                    tag = tag.ToLower();
                    if (tag.Substring(tag.Length - 2) == "an") {
                        byte[] b = HexStrToByte(data2[1]);
                        string value = System.Text.Encoding.Default.GetString(b);
                        retData += "-AN:" + value;
                    }
                }

                return retData;
            }
            catch (System.Exception ex)
            {
                return ex.Message;
            }
        }

    }

    public class scanFile
    {
        private  List<string> m_list = new List<string>();

        public List<string> getList()
        {
            return m_list;
        }

        public void ScanFiles(string directory, string fileTypeFilter)
        {
            DirectoryInfo dir = new DirectoryInfo(directory);

            //遍历一个文件夹中所有fileTypeFilter类型文件
            foreach (FileInfo file in dir.GetFiles(fileTypeFilter))
            {
                m_list.Add(file.Name);
            }
            //遍历子目录，进行递归
            foreach (DirectoryInfo childDir in dir.GetDirectories())
            {
                ScanFiles(childDir.FullName, fileTypeFilter);
            }

        }
    }
}
